package com.os.sys.util;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LngAndLatUtil {

    public static void main(String[] args){
        LngAndLatUtil.getLngAndLat("230100", "饭店");
        //LngAndLatUtil.getLngAndLatGaoDe("230100", "饭店");
        //restapi.amap.com/v3/place/text?key=您的key&keywords=北京大学&types=高等院校&city=北京&children=1&offset=20&page=1&extensions=all
    }

    /**
     * 爬取数据存入数据库-高德地图
     * @param area 查询区域
     * @param word 查询关键词
     * @return json字符串
     */
    public static JSONObject getLngAndLatGaoDe(String area, String word) {
        //高德调用说明： https://lbs.amap.com/api/webservice/guide/api/search
        Integer code = 131;
        JSONObject obj = null;
        String AK = "aba08ac19d513bce6cb5effecf0c346e"; //高德
        for (int i = 1; i < 3; i++) {
            String url = "https://restapi.amap.com/v3/place/text?key="+AK+"&keywords=北京大学&types=高等院校&city=北京&children=1&offset=20&page=1&extensions=all";
            //url ="https://api.map.baidu.com/place/v2/search?query=烧烤&region=哈尔滨&output=json&scope=2&ak="+AK;
            //url ="https://api.map.baidu.com/place/v2/search?query=烧烤&location=45.759,126.660&radius=2000&output=json&ak="+AK;
            //url ="https://api.map.baidu.com/place/v2/search?query=火锅&location=45.759,126.660&radius=2000&output=json&ak="+AK;
            //url ="https://api.map.baidu.com/place/v2/search?query=串&location=45.759,126.660&radius=2000&output=json&ak="+AK;
            //url = url +"&page_size=10&page_num="+i;
            url = "https://restapi.amap.com/v3/place/text?key="+AK+"&keywords=烧烤&types=&city=哈尔滨&children=1&offset=20&page="+(i+1)+"&extensions=base";

            String str = loadJSON(url);
            System.out.println(str);
        }
        return null;
    }
    /**
     * 爬取数据存入数据库
     * @param area 查询区域
     * @param word 查询关键词
     * @return json字符串
     */
    public static JSONObject getLngAndLat(String area, String word) {
        Integer code = 131;
        JSONObject obj = null;
        String AK = "oz9rIKGWbIHshMjol2yxmwm9BOAFZABA";
        for (int i = 0; i < 2; i++) {
            String url = "http://api.map.baidu.com/place/v2/search?query=" + word + "&tag=&scope=2®ion=" + code
                    + "&output=json&page_size=10&page_num=" + i + "&ret_coordtype=gcj02ll&ak="+AK;
            //url ="https://api.map.baidu.com/place/v2/search?query=饭店&location=39.915,116.404&radius=20000&output=json&ak="+AK;
            //url ="https://api.map.baidu.com/place/v2/search?query=饭店&region=杭州&output=json&scope=2&ak="+AK;
            //14095586.36,5710739.79
            //57.107,140.95

            url ="https://api.map.baidu.com/place/v2/search?query=烧烤&region=哈尔滨&output=json&scope=2&ak="+AK;
            //url ="https://api.map.baidu.com/place/v2/search?query=烧烤&location=45.759,126.660&radius=2000&output=json&ak="+AK;
            url ="https://api.map.baidu.com/place/v2/search?query=火锅&location=45.759,126.660&radius=2000&output=json&ak="+AK;
            //url ="https://api.map.baidu.com/place/v2/search?query=串&location=45.759,126.660&radius=2000&output=json&ak="+AK;

            url = url +"&page_size=10&page_num="+i;
            String str = loadJSON(url);
            System.out.println(str);
        }
        return null;
    }

    /**
     * 根据地址(url)获取页面数据
     * @param uri
     * @return
     */
    public static String loadJSON (String uri) {
        StringBuilder json = new StringBuilder();
        try {
            URL url = new URL(uri);
            URLConnection yc = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"utf-8"));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

}
